class GwtSellerController<GwtController
  @@LOG_FLAG="GwtSellerController"
  def index
    user_id = session[:id]
    domain_login_id = session[:domain_login_id]
    start = params[:start]
    limit = params[:limit]
    doing_express = params[:doing_express]
    if user_id==nil
      render :text => '请通过阿里平台登录后'
      return
    end
    result = Hash.new #如果doing_express不为空标示获取在途快递信息
    if doing_express==nil
      orders =WWW::Tkd::Gold::Model::TbOrder.find_seller_on_page(domain_login_id,start.to_i,limit.to_i)
      result['offset']=start.to_i
      result['count']=WWW::Tkd::Gold::Model::TbOrder.count_seller domain_login_id
    else
      #在途快递是一页显示不用分页的
      orders =WWW::Tkd::Gold::Model::TbOrder.find_seller_doing_express domain_login_id
      result['offset']=0
      result['count']=orders.size
    end
    @tb_orders=Array.new
    orders.each do|item|
      @tb_orders.push(WWW::Tkd::Gold::Model::GwtSellerOrder.init_by_order(item,user_id))
    end

    result['items']=@tb_orders
    render :text => result.to_json
  end
  #通过web进行登录
  def web
    #预先判定是否是淘宝用户
    domain_login_id=session[:domain_login_id]
    if domain_login_id==nil
      app_id='17886'
      cert_code='f14a71f0b09011ddb3e6cfd602199686'
      sip_url_pre='http://sip.alisoft.com/sip/rest'
      isv_url_pre='http://seller.taokuaidi.cn/gwt_seller/success'
      WWW::Ali.set_app_id app_id
      WWW::Ali.set_cert_code cert_code
      WWW::Ali.set_sip_url_pre sip_url_pre
      WWW::Ali.set_isv_url_pre isv_url_pre

      params[:domain_id]=3#获得淘宝自己的账号
      user = WWW::Ali::Alisoft::User.new(params,session)
      login_id = user.get_domain_login_id
      if !(login_id.instance_of? String)
        render :text=>"尊敬的用户，淘快递仅支持淘宝用户，请切换其他帐号，谢谢！"
        WWW::Tkd::Gold::Model::Logger.info  "#{@@LOG_FLAG}onlytaobao",""
        return
      end
      if user.validate_user=='1'#登录成功
        if user.get_domain_login_id==nil
          render :text => "尊敬的用户，淘快递仅支持淘宝用户，请切换其他帐号，谢谢！"
          return
        end
        tb_user = WWW::Tkd::Gold::Model::TbUser.save_from_alisoft(user,true)
        session[:user_id]=tb_user.user_id
        session[:id]=tb_user.id
        session[:sip_sessionid]=tb_user.sip_sessionid
        session[:domain_login_id]=tb_user.domain_login_id

        count = WWW::Tkd::Gold::Model::TbSellerOrder.count tb_user.id
        if count==0
          refresh_sip_sessionid
          redirect_to '/sellerGuide.html'
          return
        end
      else
        render :text => "无效登录入口或请求过期，登录阿里软件失败"
        WWW::Tkd::Gold::Model::Logger.info  "#{@@LOG_FLAG}loginalisoftfail",""
        return
      end

    end

    #获取数据并验证是否登录
    @isAPIExtendTime = is_valid_login
    @isFromWeb = true
    session[:isFromWeb]=true
    @user=WWW::Tkd::Gold::Model::TbUser.find session[:id]
  end
  def view
    #预先判定是否是淘宝用户
    domain_login_id=params['domain_login_id']
    source = params['source']#区分是否为后台登录
    is_bops = false
    if source=='bops'
      is_bops = true
    end
    if domain_login_id!=nil
      begin
        conv = Iconv.new("UTF-8","GB2312")
        domain_login_id = conv.iconv(domain_login_id)
      rescue
      end
      if   !checkname(domain_login_id)
        render :text =>'尊敬的用户，淘快递仅支持淘宝用户，请切换其他帐号，谢谢！'
        return
      end
      user_id = params['user_id']
      app_instance_id = params['app_instance_id']
      hash_code=params['hash_code']


      tb_user=WWW::Tkd::Gold::Model::TbUser.save_from_aliww(domain_login_id,user_id,app_instance_id,hash_code,true,is_bops)
      if tb_user==nil
        render :text =>'尊敬的用户，本次登录为非法'
        return
      end

      session[:user_id]=tb_user.user_id
      session[:id]=tb_user.id
      session[:sip_sessionid]=tb_user.sip_sessionid
      session[:domain_login_id]=tb_user.domain_login_id

      app_id='22531'
      cert_code='6d31f3a008d511de87188b613b005f95'
      sip_url_pre='http://sip.alisoft.com/sip/rest'
      isv_url_pre='http://seller.taokuaidi.cn/gwt_seller/success'
      WWW::Ali.set_app_id app_id
      WWW::Ali.set_cert_code cert_code
      WWW::Ali.set_sip_url_pre sip_url_pre
      WWW::Ali.set_isv_url_pre isv_url_pre
      if tb_user.is_newer#标示第一次注册用户
        refresh_sip_sessionid
        redirect_to '/sellerGuide.html'
        return
      end
    end
    if session[:id]==nil
      render :text => '请通过阿里平台登录后进行下一步操作'
      WWW::Tkd::Gold::Model::Logger.info  "#{@@LOG_FLAG}indexnolegal",""
      return
    end
    if is_bops==false #非后台登录获取数据并验证是否登录
      @isAPIExtendTime = is_valid_login
    else
      @isAPIExtendTime = true
    end
    @isFromWeb = false
    @user=WWW::Tkd::Gold::Model::TbUser.find session[:id]
  end
  #卖家手工输入订单信息
  def saveno
    user_id = session[:id]
    if user_id==nil
      result[:flag]=-1
      result[:msg]="请登录后操作"
      render :text => result.to_json
    end
    result = Hash.new
    agent_id = params[:agent_id]
    no_code=params[:no_code]
    if no_code==nil||no_code.length<6
      result[:flag]=-1
      result[:msg]="快递号请检查，谢谢啦"
      WWW::Tkd::Gold::Model::Logger.info  "#{@@LOG_FLAG}sellerexpressnotlength",""
      render :text => result.to_json
    end
    order_id=params[:order_id].to_i
    agent = WWW::Tkd::Gold::Model::Agent.find_by_id agent_id
    if agent==nil
      result[:flag]=-1
      result[:msg]="快递公司不存在"
      render :text => result.to_json
      return
    end
    #组装TbOrder对象
    seller_order = WWW::Tkd::Gold::Model::TbSellerOrder.find_from_order_id_and_user_id(order_id,user_id)
    if(seller_order==nil)
      result[:flag]=-2
      result[:msg]="交易订单非本用户"
      render :text => result.to_json
      return
    end
    #组装Express对象
    express=WWW::Tkd::Gold::Model::Express.new
    express.agent_id=agent_id
    express.no_code=no_code.strip()
    #分解对象并进行保存
    order =WWW::Tkd::Gold::Model::TbOrder.find(order_id)
    if order==nil
      result[:flag]=-3
      result[:msg]="交易订单不存在"
      render :text => result.to_json
      return
    end
    WWW::Tkd::Gold::Model::GwtSellerOrder.save seller_order,express,user_id
    result[:flag]=1
    result[:msg]="success"
    render :text => result.to_json
  end
end
